﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MMITDomain.Abstract;
using MMITDomain.Entities;

namespace MMITDomain.Concrete
{
    public class EFTransactionRepository : ITransactionRepository
    {
        EFDbContext dbContext = new EFDbContext();

        #region ITransactionRepository Members

        public IQueryable<Transaction> Transactions
        {
            get
            {
                return dbContext.Transactions;
            }
        }

        public void SaveTransaction(Transaction objTransaction)
        {
            if(objTransaction.Id == 0)
            {
                dbContext.Transactions.Add(objTransaction);
            }

            dbContext.SaveChanges();
        }

        public void DeleteTransaction(Transaction objTransaction)
        {
            dbContext.Transactions.Remove(objTransaction);
            dbContext.SaveChanges();
        }

        public Transaction GetTransactionById(int iTransactionId)
        {
            return dbContext.Transactions.Find(iTransactionId);
        }

        public IQueryable<Transaction> GetCustomerTransactions(Customer objCustomer)
        {
            return dbContext.Transactions.Where(t => t.Sender.CustomerId == objCustomer.Id);
        }

        public IQueryable<Transaction> GetTransactionsByType(TransactionType objType)
        {
            return dbContext.Transactions.Where(t => t.TransactionType == objType);
        }

        public Transaction GetTransactionByCode(string strTransactionCode)
        {
            return dbContext.Transactions.Where(t => t.TransactionCode == strTransactionCode).FirstOrDefault();
        }
        #endregion
    }
}
